package com.farm.system.dao;

import com.farm.system.pojo.Role;
import com.farm.system.pojo.User;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import tk.mybatis.mapper.common.Mapper;

import java.util.Set;

/**
 * 用户DAO
 *
 * @author Chengfei
 * @date 2020/10/10
 */
public interface UserMapper extends Mapper<User> {

    /**
     * 添加用户-角色表
     *
     * @param userId 用户id
     * @param roleId 角色id
     */
    @Insert("insert into t_user_role values(#{userId},#{roleId})")
    void setUserIdAndRoleId(@Param("userId") Integer userId, @Param("roleId") Integer roleId);

    /**
     * 根据用户id删除用户-角色表
     *
     * @param userId 用户id
     */
    @Delete("delete from t_user_role where uid=#{userId}")
    void deleteAssociation(Integer userId);


    /**
     * 根据用户id查询角色
     * @param userId 用户名
     * @return 角色集合
     */
    @Select("select tr.* from t_user_role tur , t_role tr where tur.uid = #{userId} and tur.rid = tr.id ")
    Set<Role> findRoleByUserId(Integer userId);

}
